<template>
  <div>
    <el-row >

    <el-col :span="14"><div class="grid-content bg-purpl">
      <img src="../imgs/bg-register.jpg" alt=""/></div>
      
    </el-col>
   <el-col :span="10">
    <div class="login-container grid-content bg-purple-light" >
    <el-row type="flex" class="row-bg" justify="center">
      <h2 style="">系统登录</h2>
    </el-row>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="70px" class="demo-ruleForm">
      <el-form-item label="账号" prop="username">
    <el-input autofocus v-model="ruleForm.userName"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
  </el-form-item>
 
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>

  </div>
</el-col>
  </el-row>
 </div>

</template>

<script>
// @ is an alias to /src
import HelloWorld from "@/components/HelloWorld.vue";
import "@/css/login.css"
export default {
  name: "login",
  components: {
    HelloWorld,
  },
  data() {
      var validateUserName = (rule, value, callback) => {
        if (value==='') {
          callback(new Error('账号不能为空'));
        }else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
  
      };
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      return {
        ruleForm: {
          userName: '',
          password: ''
        },
        rules: {
          userName: [
            { validator: validateUserName, trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
        
          if (valid) {
           const loading = this.$loading({
          lock: true,
          text: '登录中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
            this.axios.get("api/hj/loginQuery",{params:{
              name:this.ruleForm.userName,
              password:this.ruleForm.password
            }}).then((result)=>{
             console.log(result.data)
              if(result.data!=""){
                  loading.close();
                  sessionStorage.setItem("userName",JSON.stringify(result.data))
                  this.$message({
                      message: '欢迎'+result.data.user_name+'登录健康系统',
                      type: 'success'
                    });
                this.$router.push("/home");
              }else{
                loading.close();
                
                this.$message.error('账号或密码错误');
            
              }
            })
           
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
  }

</script>

<style scoped>
  .login-container {
  /* border-radius: 15px; */
 background-clip: padding-box;
 margin: 180px auto;
 width: 350px;
 padding: 35px 35px 15px 35px;
 background: #fff;
  border: 1px solid #eaeaea;
 box-shadow: 0 0 25px #cac6c6;
}
</style>